﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using CI.Interfaces;
using CI.Organism;
using CI.WebService;

namespace CI.PW.New_Scenario_Wizard
{
    /// <summary>
    /// Interaction logic for OrganismSelection.xaml
    /// </summary>
    public partial class OrganismSelection
    {
        private readonly string _scenarioName;
        private IScenario _scenario;
        private readonly Dispatcher _dispatcher;
        public readonly BindingList<ScenarioOrganism> ScenarioOrganisms;
        private Progress _progress;
        private SimEnv _simEnv = SimEnv.Instance;
        public OrganismSelection(string scenarioName)
        {
            InitializeComponent();
            ScenarioOrganisms = new BindingList<ScenarioOrganism>();
            ScenarioOrganismList.ItemsSource = ScenarioOrganisms;
            _scenarioName = scenarioName;
            _progress = new Progress();
            _progress.Show();
            _dispatcher = Dispatcher.CurrentDispatcher;
            ThreadPool.QueueUserWorkItem(o => LoadScenarioOrganisms(), null);
        }

        private void LoadScenarioOrganisms()
        {
            SimEnv.Instance.TraceLogger.LogDebug("GetScenarioOrganisms - Start");

            // get the list of scenarios
            var sf = new Mesopotamia.ScenarioFactory();
            _scenario = sf.GetScenario();
            _scenario.ScenarioName = _scenarioName;
            _scenario.Load();
            var results = _scenario.GetScenarioOrganisms(_scenarioName, 0, 1000);
            _dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate
            {
                if (_progress != null)
                    _progress.Close();

                _progress = null;

                ScenarioOrganisms.Clear();

                if (results == null)
                    return;

                foreach (var result in results.Organisms)
                {
                    ScenarioOrganisms.Add(result);
                }

                SimEnv.Instance.TraceLogger.LogDebug("GetScenarioOrganisms - End - Count : " + ScenarioOrganisms.Count);
            });


        }

        private void StartWithOrganisms(object sender, RoutedEventArgs e)
        {

             Display.SelectedOrganismsToLoad.Clear();

            var maxOrganismsAllowed = _scenario.FallBackOrganisms.Count;

            if (ScenarioOrganismList.SelectedItems.Count > maxOrganismsAllowed)
                MessageBox.Show(string.Format("You can only Select {0} items for this scenario", maxOrganismsAllowed), "Error");
            else
            {
                // todo : start with select organisms
                foreach (var scenarioOrganism in ScenarioOrganismList.SelectedItems)
                {
                    var organismGuid = ((ScenarioOrganism)scenarioOrganism).Guid;
                    Display.SelectedOrganismsToLoad.Add(organismGuid);
                    SimEnv.Instance.TraceLogger.LogDebug("StartWithOrganisms - Requested Organism with GUID : " + organismGuid);
                }

                // restart if not running
                if ((_simEnv.HostEnv != null) && (!_simEnv.HostEnv.StartTime.Equals(new DateTime())))
                {
                    _simEnv.TriggerScenarioRestart("Start Manually Selected Simulation");
                }

                ((Window)Parent).Close();
            }
        }
    }
}
